package com.md.api.service.impl;

import com.md.api.common.utils.TokenUtil;
import com.md.api.dao.*;
import com.md.api.entity.*;
import com.md.api.entity.dto.ComposeCommitDto;
import com.md.api.entity.dto.ComposeQueryDto;
import com.md.api.entity.enums.ChangeStatus;
import com.md.api.entity.enums.DrinkingStatus;
import com.md.api.entity.enums.PayStatus;
import com.md.api.entity.enums.ShoppingStatus;
import com.md.api.entity.util.Page;
import com.md.api.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Author liujie
 * @ClassName VipCenterServiceImpl
 **/

@Slf4j
@Service
public class OrderServiceImpl implements OrderService {


    @Resource
    ShoppingRefundOrderMapper shoppingRefundOrderMapper;
    @Resource
    ShoppingOrderMapper shoppingOrderMapper;
    @Resource
    TokenUtil tokenUtil;
    @Resource
    ShoppingOrderDetailMapper shoppingOrderDetailMapper;
    @Resource
    ShoppingChangeOrderMapper changeOrderMapper;

    @Override
    public HttpResult myShoppingOrder(HttpServletRequest request, ComposeQueryDto qto) {
        Page page = qto.getPage();
        page.initPagingData(ShoppingOrder.class);
        String userId = tokenUtil.getUserId(request);
        List<ShoppingOrder> list = shoppingOrderMapper.list(userId,qto.getPayStatus(),qto.getOrderType(),qto.getPayType(),qto.getPayTimeStart(),qto.getPayTimeEnd());
        page.setResult(list);
        return new HttpResult(page);
    }



    @Override
    public HttpResult confirmShoppingOrder(HttpServletRequest request, ComposeQueryDto qto) {
        String userId = tokenUtil.getUserId(request);
        ShoppingOrder byOrderNo = shoppingOrderMapper.getByOrderNo(qto.getOrderNo());
        if (byOrderNo.getUserId().equals(userId)) {
            shoppingOrderMapper.confirmOrder(qto.getOrderNo(),qto.getSkuId(),1);
            ShoppingOrderDetail detail = shoppingOrderDetailMapper.getDetail(qto.getOrderNo(),qto.getSkuId());
            detail.setStatus(ShoppingStatus.COMPLETED.value());
            //如果是换货，修改换货状态
            ShoppingChangeOrder dbchangeOrder =changeOrderMapper.getDetail(qto.getOrderNo(),qto.getSkuId());
            if (null!=dbchangeOrder){
                dbchangeOrder.setChangeStatus(ChangeStatus.COMPLETED.value());
                changeOrderMapper.update(dbchangeOrder);
                detail.setStatus(ShoppingStatus.CHANGE_COMPLETED.value());
            }
            shoppingOrderDetailMapper.updateStatus(detail);
        }
        return HttpResult.OK;
    }

    @Override
    public HttpResult deleteOrder(HttpServletRequest request, ComposeCommitDto qto) {
        String userId = tokenUtil.getUserId(request);
        ShoppingOrder dbOrder = shoppingOrderMapper.getByOrderNo(qto.getOrderNo());
        if (dbOrder.getUserId().equals(userId)){
            shoppingOrderMapper.deleteByOrderNo(qto.getOrderNo());
        }else {
            return HttpResult.fail("該訂單不屬於您！");
        }

        return HttpResult.OK;
    }



    @Override
    public HttpResult shoppingOrderDetail(HttpServletRequest request, ComposeCommitDto qto) {
        ShoppingOrder dbOrder = shoppingOrderMapper.getByOrderNo(qto.getOrderNo());
        List<ShoppingOrderDetail> details = shoppingOrderMapper.orderDetail(qto.getOrderNo());
        if (!CollectionUtils.isEmpty(details)){
            for (ShoppingOrderDetail detail:details){
                ShoppingRefundOrder refundOrder = shoppingRefundOrderMapper.getDetail(qto.getOrderNo(), detail.getSkuId());
                detail.setRefundOrder(refundOrder);
            }
            dbOrder.setDetailList(details);
        }
        return new HttpResult(dbOrder);
    }



}
